/*
    This file is part of Emu.

    Emu is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Emu is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Emu.  If not, see <http://www.gnu.org/licenses/>. */
#include <QDialogButtonBox>
#include <QGridLayout>
#include <QHeaderView>

/* colordefs.h for C_NUM_COLORS. */
#include "ecolordefs.h"
#include "esessioneditor.h"

/* Includes for the managing widgets. */
#include "ehosteditor.h"
#include "elistpropeditor.h"
#include "edisplayeditor.h"

ESessionEditor::ESessionEditor(EProfile *p, QWidget *parent)
    : QDialog(parent)
{
    /* This is used by the display manager to show what colors have changed. */
    _changes = new bool[32];
    memset(_changes, false, sizeof(bool) * 32);
    _needReset = false;

    _sessPrefs = p;
    _newPrefs = new EProfile(*_sessPrefs);

    QDialogButtonBox *box = new QDialogButtonBox(QDialogButtonBox::Cancel |
                                                 QDialogButtonBox::Ok);

    /* Make the navigation tree and display, then setup the stuff inside. */
    setupNavAndDisplay();

    QHBoxLayout *horizLayout = new QHBoxLayout;
    QVBoxLayout *layout = new QVBoxLayout;
    horizLayout->addWidget(_navTree);
    horizLayout->addWidget(_display);
    /* Allow as little padding as possible, since it's useless. */
    horizLayout->setSpacing(0);

    layout->addLayout(horizLayout);
    layout->addWidget(box);

    connect(_navTree,
            SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
            this, SLOT(navItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
    connect(box, SIGNAL(accepted()), this, SLOT(applyChanges()));
    connect(box, SIGNAL(rejected()), this, SLOT(reject()));

    _navTree->setCurrentItem(_navTree->topLevelItem(0));

    setLayout(layout);
    setWindowTitle(tr("Emu Profile Editor"));
}

ESessionEditor::~ESessionEditor()
{
    delete[] _changes;
    delete _newPrefs;
}

void ESessionEditor::applyChanges()
{
    int i;
    for (i = 0;i < _display->count();i++)
        static_cast<EEditorWidget *>(_display->widget(i))->applyPrefChanges();

    _sessPrefs->setHostName(_newPrefs->hostName());
    _sessPrefs->setHostAddress(_newPrefs->hostAddress());
    _sessPrefs->setHostPort(_newPrefs->hostPort());
    _sessPrefs->setAliases(_newPrefs->aliases());
    _sessPrefs->setMacros(_newPrefs->macros());
    _sessPrefs->setTriggers(_newPrefs->triggers());

    if (_needReset) {
        /* Go through the colors and fix the ones that have been changed. */
        for (i = 0;i < C_NUM_COLORS;i++) {
            if (_changes[i])
                _sessPrefs->setColor(i, _newPrefs->color(i));
        }
    }

    accept();
}

void ESessionEditor::navItemChanged(QTreeWidgetItem *curr,
                                    QTreeWidgetItem *prev)
{
    Q_UNUSED(prev)

    if (curr != NULL)
        _display->setCurrentIndex(_navTree->indexOfTopLevelItem(curr));
}

void ESessionEditor::setupNavAndDisplay()
{
    _display = new QStackedWidget;
    _navTree = new QTreeWidget;
    _display->addWidget(new EHostEditor(_newPrefs));
    _display->addWidget(new EDisplayEditor(_newPrefs, &_changes, &_needReset));
    _display->addWidget(new EListPropEditor(_newPrefs, EPropAliases));
    _display->addWidget(new EListPropEditor(_newPrefs, EPropMacros));
    _display->addWidget(new EListPropEditor(_newPrefs, EPropTriggers));

    _navTree->header()->setVisible(false);
    _navTree->header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
    _navTree->setIndentation(0);

    QStringList navList = QStringList() << tr("Host") << tr("Display")
                           << tr("Aliases") << tr("Macros") << tr("Triggers");

    QStringList::const_iterator i;
    for (i = navList.constBegin();i != navList.constEnd();i++) {
        QTreeWidgetItem *newItem = new QTreeWidgetItem;
        newItem->setText(0, *i);
        _navTree->addTopLevelItem(newItem);
    }

    /* This makes sure the navigation tree stays as small as possible without
       having a scrollbar. */
    QMargins m = _navTree->contentsMargins();
    _navTree->setFixedWidth(_navTree->columnWidth(0) + m.left() + m.right());
}
